package com.ruoyi.erp.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.BatchReceivabelPlan;
import com.ruoyi.erp.domain.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ReceivablePlanMapper;
import com.ruoyi.erp.domain.ReceivablePlan;
import com.ruoyi.erp.service.IReceivablePlanService;
import com.ruoyi.common.core.text.Convert;

/**
 * 收款计划Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-28
 */
@Service
public class ReceivablePlanServiceImpl implements IReceivablePlanService 
{
    @Autowired
    private ReceivablePlanMapper receivablePlanMapper;

    /**
     * 查询收款计划
     * 
     * @param id 收款计划主键
     * @return 收款计划
     */
    @Override
    public ReceivablePlan selectReceivablePlanById(Integer id)
    {
        return receivablePlanMapper.selectReceivablePlanById(id);
    }

    /**
     * 查询收款计划列表
     * 
     * @param receivablePlan 收款计划
     * @return 收款计划
     */
    @Override
    public List<ReceivablePlan> selectReceivablePlanList(ReceivablePlan receivablePlan)
    {
        return receivablePlanMapper.selectReceivablePlanList(receivablePlan);
    }

    /**
     * 新增收款计划
     * 
     * @param receivablePlan 收款计划
     * @return 结果
     */
    @Override
    public int insertReceivablePlan(ReceivablePlan receivablePlan)
    {
        receivablePlan.setCreateTime(DateUtils.getNowDate());
        return receivablePlanMapper.insertReceivablePlan(receivablePlan);
    }

    /**
     * 修改收款计划
     * 
     * @param receivablePlan 收款计划
     * @return 结果
     */
    @Override
    public int updateReceivablePlan(ReceivablePlan receivablePlan)
    {
        receivablePlan.setUpdateTime(DateUtils.getNowDate());
        return receivablePlanMapper.updateReceivablePlan(receivablePlan);
    }

    /**
     * 批量删除收款计划
     * 
     * @param ids 需要删除的收款计划主键
     * @return 结果
     */
    @Override
    public int deleteReceivablePlanByIds(String ids)
    {
        return receivablePlanMapper.deleteReceivablePlanByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收款计划信息
     * 
     * @param id 收款计划主键
     * @return 结果
     */
    @Override
    public int deleteReceivablePlanById(Integer id)
    {
        ReceivablePlan existPlan = this.selectReceivablePlanById(id);
        if (!StringUtils.isNull(existPlan.getPayedAmount()) && existPlan.getPayedAmount().compareTo(BigDecimal.ZERO) == 1){
            throw new ServiceException("不能删除已有收款记录的计划！");
        }
        return receivablePlanMapper.deleteReceivablePlanById(id);
    }

    /**
     * 批量新增一个项目的收款计划
     *
     * @param project
     * @param batchReceivabelPlan
     * @return
     */
    @Override
    public int batchInsertReceivablePlans(Project project, BatchReceivabelPlan batchReceivabelPlan) {
        if(!StringUtils.isNull(batchReceivabelPlan.getPlans()))
        {
            batchReceivabelPlan.getPlans().stream().filter(f -> f.getExpectAmount().compareTo(BigDecimal.ZERO) != 0).forEach(item -> {
               item.setProjectId(project.getId());
               item.setBusinessUnitId(project.getBusinessUnitId());
               item.setCustomerId(project.getCustomerId());
               item.setCreateTime(DateUtils.getNowDate());
               item.setCognizanceType("stock");
               item.setInvoiceStatus("N");
               item.setPlanStatus("expect");
               this.insertReceivablePlan(item);
            });
            return batchReceivabelPlan.getPlans().size();
        }else {
            return 0;
        }

    }
}
